package com.vancouver.knows.ui.activity.my;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.pangu.AMApplication;
import com.pangu.task.mark.ATaskMark;
import com.pangu.ui.browser.ListItemBrowser;
import com.vancouver.knows.MainApplication;
import com.vancouver.knows.R;
import com.vancouver.knows.business.BusinessModule;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by llf on 2016/4/27.
 * 积分明细列表
 */
public class MyPointsDetailView extends ListItemBrowser<AMApplication> {

    private String[] pointsDetailList;
    private String totalPoints;
    private String[] pointsDetailArr;
    private String pointsStore;
    private Context mContext;

    public MyPointsDetailView(Context context, String[] detailList, String totalPoints, String pointsStore) {
        super(context);
        mContext = context;
        this.pointsDetailList = detailList;
        this.totalPoints = totalPoints;
        this.pointsStore = pointsStore;
        pointsDetailArr = context.getResources().getStringArray(R.array.detail_points);
        loadData();
    }

    private void loadData() {
        BusinessModule module = ((MainApplication) imContext).getBusinessModule();
        ATaskMark aTaskMark = module.getTaskMarkPool().getMyPointsDetailTaskMark();
        if (aTaskMark.getTaskStatus() == ATaskMark.HANDLE_OVER) {
            aTaskMark.reinitTaskMark();
        }
        aTaskMark.setTaskStatus(ATaskMark.HANDLE_OVER);
        MyPointsDetailView.this.initLoadableView(aTaskMark);
    }

    @Override
    protected BaseAdapter createAdapter() {
        return new PointsDetailListAdapter();
    }

    @Override
    protected void handleLoadNewItems(ATaskMark taskMark) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        return false;
    }

    public class PointsDetailListAdapter extends BaseAdapter {

        private LayoutInflater lf;

        public PointsDetailListAdapter(){
            this.lf = LayoutInflater.from(getContext());
        }

        @Override
        public int getCount() {
            if (pointsDetailList != null && pointsDetailList.length > 0) {
                return pointsDetailList.length;
            } else {
                return 0;
            }
        }

        @Override
        public String getItem(int position) {
            return pointsDetailList[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            ViewValueHand hand = null;
            View view = convertView;
            if (convertView == null) {
                view = lf.inflate(R.layout.points_details, parent, false);
                hand = new ViewValueHand(view);

                view.setTag(hand);
            } else {
                hand = (ViewValueHand) view.getTag();
            }

            if (position == getCount() - 1) {
                hand.pointsDetailTxt.setText(Html.fromHtml(pointsDetailArr[position] + "<font color=\"#f26a43\">" + pointsDetailList[position] + "</font>" +
                        mContext.getString(R.string.point2)));
                hand.detailPercentageTxt.setText(Html.fromHtml("<font color=\"#f26a43\">"+mContext.getString(R.string.point_do_what)+"</font>"));
            }else {
                hand.pointsDetailTxt.setText(Html.fromHtml(pointsDetailArr[position] + "<font color=\"#f26a43\">" + pointsDetailList[position] + "</font>" +
                        mContext.getString(R.string.point1)));
                try {
                    int total = Integer.parseInt(totalPoints);
                    if (total != 0) {
                        int percentage = Integer.parseInt(getItem(position)) * 100 / total;
                        hand.detailPercentageTxt.setText(mContext.getString(R.string.point_percentage, percentage + "%"));
                    } else {
                        hand.detailPercentageTxt.setText(mContext.getString(R.string.point_percentage, total + "%"));
                    }
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }

            hand.detailPercentageTxt.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (position == (getCount() - 1)) {
                        if (pointsStore != null) {
                            Intent intent = new Intent(mContext, MyPointsWebViewActivity.class);
                            intent.putExtra("USER_NOTICE", mContext.getString(R.string.points_shop));
                            intent.putExtra("POINT_URL", pointsStore);
                            mContext.startActivity(intent);
                        }
                    }
                }
            });

            return view;
        }

        public class ViewValueHand {
            @Bind(R.id.point_details)
            TextView pointsDetailTxt;
            @Bind(R.id.details_percentage)
            TextView detailPercentageTxt;

            public ViewValueHand(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }
}
